<?php
namespace app\index\controller;

use app\index\logic\LoginLogic;
use app\index\model\SmsModel;
use app\index\util\Response;
use think\Cookie;
use think\Request;
use think\Session;

class Login extends Base
{
    protected $_check_login = [
        'logout'
    ];
    public function index()
    {
        if(Session::get('user')){
            $this->redirect('/index/user/index');
        }
        return view();
    }
    public function login(){
       $mobile = Request::instance()->request('mobile');
       if($mobile == ''){
           Response::json(-1,'手机号码不能为空');
       }
       $sms_code = Request::instance()->request('code');
        if($sms_code == ''){
            Response::json(-1,'手机验证码不能为空');
        }
       $model = new SmsModel();
       $result = $model->use_code($mobile,$sms_code);
       if($result === false){
           Response::json(-1,'验证码错误');
       }
       $logic = new LoginLogic();
       if($logic->login($mobile)){
           Response::json(1);
       }else{
           Response::json(-1,$logic->get_error());
       }

    }
    public function logout(){
        Session::delete('user');
        Cookie::delete('user');
        $this->redirect(url('index/index'));
    }
}
